"""
目标: 爬取首页logo
pip install requests
pip install lxml
pip install beautifulsoup4
"""
import requests
from requests import Response
from bs4 import BeautifulSoup, Tag

URL: str = "https://www.baidu.com"


def main():
    # 1、获取百度首页内容
    headers: dict = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
    }
    response: Response = requests.get(url=URL, headers=headers)
    # 百度首页编码为utf-8
    response.encoding = "utf-8"

    # 2、解析HTML，查找logo图片，根据浏览器开发者工具定位到logo图片的id是s_lg_img
    soup: BeautifulSoup = BeautifulSoup(response.text, "html.parser")

    img_list: list = soup.find_all("img", attrs={"id": "s_lg_img"})

    # 获取logo的src，并拼接为完整的https链接
    img_src: str = ""
    if img_list:
        img_info: Tag = img_list[0]
        img_src = f"https:{img_info.get("src")}"

    # 3、保存图片
    img_data: bytes = b""
    if img_src:
        img_response: Response = requests.get(url=img_src, headers=headers)
        img_data: bytes = img_response.content

    if img_data:
        with open("./Python进阶训练营/week01/baidu.png", "wb") as f:
            f.write(img_data)
            print("图片下载成功！")


if __name__ == "__main__":
    main()
